<?php

include_once("common.php");

$title= "Uploading";
$metaKey=null;
$metaDesc="Upload photos to homepage.";
$moreHtml=null;
$moreJava=null;

function republishPhoto($id){
	connectSelectTest("homePagePhotos");
	mysql_query("UPDATE `homePagePhotos` SET `dateUploaded`='".date("Y-m-d H:i:s", strtotime("now"))."' WHERE `id`='".$id."'");
	printf("<script type='text/javascript'>window.location = \"/upload.php\";</script>");
}

startPage($title, $metaKey, $metaDesc, $moreHtml, $moreJava, $title);

if ($_REQUEST['uploadCookie'] != "1024x768"){
	
	printf("<div class='autoBox' style='width:%s; padding:20px;'>\n", "100%");
	printf("<form style='margin-left:%s;'>\n", "39%");
	printf("<input type='password' value='%s' placeholder='password' name='pass' />\n", $_GET['pass']);
	printf("<input type='submit' value='¡log in!' />\n", $_GET['pass']);
	printf("</form>\n");
	printf("</div>\n");
	
	if ($_GET['pass'] == "chickens"){
		printf("<script type='text/javascript'>document.cookie = \"uploadCookie=1024x768\"</script>");
		printf("<script type='text/javascript'>window.location = \"/upload.php\";</script>");
	}
}
else{
	printf("<div class='autoBox' style='width:%s; text-align:center; padding:20px;'>\n", "100%");
	if (($_FILES["pic"]["type"] == "image/gif") || ($_FILES["pic"]["type"] == "image/jpeg") || ($_FILES["pic"]["type"] == "image/png") || ($_FILES["pic"]["type"] == "image/pjpeg") && ($_FILES['pic']['size'] <= 100000)){
		if ($_FILES["pic"]["error"] > 0){
			 printf("<p>Error Code: %s</p>", $_FILES["pic"]["error"]);
		}
		else {		
			if (file_exists("images/homePage/".$_FILES["pic"]["name"])) {
			  	printf("<p>%s already exists.</p>", $_FILES["pic"]["name"]);
			}
			else {
			  	move_uploaded_file($_FILES["pic"]["tmp_name"], "images/homePage/".$_FILES["pic"]["name"]);
			  	connectSelectTest("homePagePhotos");
			  	mysql_query("INSERT INTO `homePagePhotos` VALUES('','".$_POST['title']."','/images/homePage/".$_FILES['pic']['name']."','".$_POST['dateUploaded']."')");
			  	printf("<p>¡Uploaded %s!<br><a id='onBlue' href='upload.php'>upload more</a> | <a id='onBlue' href='/'>see your work</a></p>", $_POST['title']);
			}
		}
	}
	else {
		printf("<p>¡¡¡Invalid file!!!<br>Must be under 100Kb and either .jpg .jpeg or .png</p>");
	}
	printf("</div>\n");
}
endPage();